/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanforpi.client.presenter;

import br.com.sales.generics.client.HanAbstractPresenter;
import br.com.sales.generics.client.nametoken.NameTokens;
import br.com.sales.generics.client.request.HannaRequestCallback;
import br.com.sales.generics.client.utils.User;
import br.com.sales.hanforpi.client.presenter.constant.HanCadPIConst;
import static br.com.sales.hanforpi.client.presenter.constant.HanCadPIConst.PASSWORD;
import static br.com.sales.hanforpi.client.presenter.constant.HanCadPIConst.USER;
import br.com.sales.hanforpi.client.service.HanForPIService;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

/**
 *
 * @author v
 */
public class HanForPIPresenter
        extends HanAbstractPresenter<HanForPIPresenter.Display, HanForPIPresenter.MyProxy>
        implements HanForPIUiHandler {

    @Inject
    private User user;
    @Inject
    private HanForPIService service;

    @Inject
    public HanForPIPresenter(EventBus eventBus,
            HanForPIPresenter.Display view,
            HanForPIPresenter.MyProxy proxy) {
        super(eventBus, view, proxy);
        getView().setUiHandlers(HanForPIPresenter.this);
    }

    @Override
    protected void onBind() {
        super.onBind();
        getView().buildView();
    }

    @Override
    public void onClik() {
        if ((getView().getNmUser().getValue() == null || "".equals(getView().getNmUser().getValue()))
                || (getView().getPassword().getValue() == null || "".equals(getView().getPassword().getValue()))) {
            getView().getMsgError().setVisible(true);
            return;
        }
        validateLogin();
    }

    @Override
    protected void onReveal() {
        super.onReveal();
        getView().getPassword().setValue("");
        getView().getNmUser().setValue("");
        getView().getNmUser().setFocus(true);
        getView().getMsgError().setVisible(false);
    }

    private void validateLogin() {
        service.validateLogin(getView().getNmUser().getText(), getView().getPassword().getText(), new HannaRequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                JSONObject record = new JSONObject(JsonUtils.safeEval(response.getText()));

                if (Integer.parseInt(record.get("RETORNO").toString()) == 1) {
                    if ("A".equals(record.get("SITUACAO").isString().stringValue())) {
                        user.setNmUser(getView().getNmUser().getText());
                        user.setIsManager(Integer.parseInt(record.get("TIPO_USUARIO").toString()) == 1);
                        revealPlace(NameTokens.CONTROLLER);

                    } else {
                        getView().getMsgError().setText(HanCadPIConst.MSG_USUARIO_INATIVO);
                        getView().getMsgError().setVisible(true);
                    }
                } else if (USER.equals(getView().getNmUser().getText())
                        || PASSWORD.equals(getView().getPassword().getText())) {
                    user.setNmUser(USER);
                    user.setIsManager(true);
                    revealPlace(NameTokens.CONTROLLER);
                } else {
                    getView().getMsgError().setText(HanCadPIConst.MSG_USUARIO_INVALIDO);
                    getView().getMsgError().setVisible(true);
                }
            }

        });
    }

    @Override
    protected void revealInParent() {
        RevealRootContentEvent.fire(this, this);
    }

    @ProxyCodeSplit
    @NameToken(NameTokens.LOGIN)
    public interface MyProxy extends ProxyPlace<HanForPIPresenter> {
    }

    public interface Display extends View, HasUiHandlers<HanForPIPresenter> {

        void buildView();

        TextBox getPassword();

        TextBox getNmUser();

        Label getMsgError();
    }
}
